package com.ima.web.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ima.web.beans.db.Users;
import com.ima.web.dao.Dao;
import com.ima.web.dao.UsersDao;

@Repository
public class UsersDaoImpl implements UsersDao{

	
	private SessionFactory sessionFactory;
	private Logger log;
	
	public UsersDaoImpl(){
		log = Logger.getLogger(this.getClass());
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public void save(Users data) {
		Session s = sessionFactory.getCurrentSession();
		s.save(data);
		
	}

	@Override
	public void delete(Users data) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Users findById(int id) {
		Session s = sessionFactory.getCurrentSession();
		
		Users u = (Users)s.load(Users.class, id); 
		log.debug("findById: "+u.getPenName());
		
		//s.update(u);
		return u;
	}

	@Override
	public List<Users> list(Users data) {
		Session s = sessionFactory.getCurrentSession();
		
		Criteria c = s.createCriteria(Users.class);
		c.add(Example.create(data));
		
		log.debug("DAO: "+data.getUsername());
		
		List<Users> u = c.list();
		//s.update(u);		
		
		System.out.println(u.size());
		return u;
	}

}
